package com.ruoyi.project.dict.hanyuzi.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.dict.hanyuzi.domain.HanyuZi;
import com.ruoyi.project.dict.hanyuzi.service.IHanyuZiService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 百度汉字Controller
 *
 * @author shixiangwen
 * @date 2021-01-29
 */
@Controller
@RequestMapping("/dict/hanyuzi")
public class HanyuZiController extends BaseController {

    private String prefix = "dict/hanyuzi";

    @Autowired
    private IHanyuZiService hanyuZiService;

    @RequiresPermissions("dict:hanyuzi:view")
    @GetMapping()
    public String hanyuzi() {
        return prefix + "/hanyuzi";
    }

    /**
     * 查询百度汉字列表
     */
    @RequiresPermissions("dict:hanyuzi:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HanyuZi hanyuZi) {
        startPage();
        List<HanyuZi> list = hanyuZiService.selectHanyuZiList(hanyuZi);
        return getDataTable(list);
    }

    /**
     * 导出百度汉字列表
     */
    @RequiresPermissions("dict:hanyuzi:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HanyuZi hanyuZi) {
        List<HanyuZi> list = hanyuZiService.selectHanyuZiList(hanyuZi);
        ExcelUtil<HanyuZi> util = new ExcelUtil<HanyuZi>(HanyuZi.class);
        return util.exportExcel(list, "hanyuzi");
    }

    /**
     * 新增百度汉字
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存百度汉字
     */
    @RequiresPermissions("dict:hanyuzi:add")
    @Log(title = "百度汉字", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HanyuZi hanyuZi) {
        return toAjax(hanyuZiService.insertHanyuZi(hanyuZi));
    }

    /**
     * 修改百度汉字
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        HanyuZi hanyuZi = hanyuZiService.selectHanyuZiById(id);
        mmap.put("hanyuZi", hanyuZi);
        return prefix + "/edit";
    }

    /**
     * 修改保存百度汉字
     */
    @RequiresPermissions("dict:hanyuzi:edit")
    @Log(title = "百度汉字", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HanyuZi hanyuZi) {
        return toAjax(hanyuZiService.updateHanyuZi(hanyuZi));
    }

    /**
     * 删除百度汉字
     */
    @RequiresPermissions("dict:hanyuzi:remove")
    @Log(title = "百度汉字", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hanyuZiService.deleteHanyuZiByIds(ids));
    }

    /**
     * 同步到生字库
     */
    @RequiresPermissions("dict:hanyuzi:edit")
    @Log(title = "百度汉字", businessType = BusinessType.UPDATE)
    @PostMapping("/import2zi")
    @ResponseBody
    public AjaxResult import2zi(Long id) {
        return toAjax(hanyuZiService.impirt2zi(id));
    }

    /**
     * 批量同步到生字库
     */
    @RequiresPermissions("dict:hanyuzi:edit")
    @Log(title = "百度汉字", businessType = BusinessType.UPDATE)
    @PostMapping("/batchimport2zi")
    @ResponseBody
    public AjaxResult batchimport2zi(String ids) {
        Long[] idList = Convert.toLongArray(ids);
        for (Long id : idList) {
            hanyuZiService.impirt2zi(id);
        }
        return toAjax(true);
    }

}
